[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter describes no additional features of Emacs Lisp. Instead it gives advice on making effective use of the features described in the previous chapters.
A.1 Writing Clean Lisp Programs | Writing clean and robust programs. | |
A.2 Tips for Making Compiled Code Fast | Making compiled code run fast. | |
A.3 Tips for Documentation Strings | Writing readable documentation strings. | |
A.4 Tips on Writing Comments | Conventions for writing comments. | |
A.5 Conventional Headers for Emacs Libraries | Standard headers for library packages. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here are some tips for avoiding common errors in writing Lisp code intended for widespread use:
This recommendation applies even to names for traditional Lisp
primitives that are not primitives in Emacs Lisp—even to cadr
.
Believe it or not, there is more than one plausible way to define
cadr
. Play it safe; append your name prefix to produce a name
like foo-cadr
or mylib-cadr
instead.
If one prefix is insufficient, your package may use two or three alternative common prefixes, so long as they make sense.
Separate the prefix from the rest of the symbol name with a hyphen, ‘-’. This will be consistent with Emacs itself and with most Emacs Lisp programs.
provide
in each separate
library program, at least if there is more than one entry point to the
program.
(require 'bar)
before the first
use of the macro. (And bar should contain (provide
'bar)
, to make the require
work.) This will cause
bar to be loaded when you byte-compile foo. Otherwise, you
risk compiling foo without the necessary macro loaded, and that
would produce compiled code that won’t work right. @xref{Compiling
Macros}.
run-hooks
, just as the existing major modes do. @xref{Hooks}.
Instead, define sequences consisting of C-c followed by a non-letter. These sequences are reserved for major modes.
Changing all the major modes in Emacs 18 so they would follow this convention was a lot of work. Abandoning this convention would waste that work and inconvenience the users.
next-line
or previous-line
in programs; nearly
always, forward-line
is more convenient as well as more
predictable and robust. @xref{Text Lines}.
In particular, don’t use these functions:
beginning-of-buffer
, end-of-buffer
replace-string
, replace-regexp
If you just want to move point, or replace a certain string, without any of the other features intended for interactive users, you can replace these functions with one or two lines of simple Lisp code.
message
function, not princ
. @xref{The Echo Area}.
error
(or signal
). The function error
does not return.
@xref{Signaling Errors}.
Do not use message
, throw
, sleep-for
,
or beep
to report errors.
edit-options
command does: switch to another buffer and let the user switch back at
will. @xref{Recursive Editing}.
indent-sexp
) using the
default indentation parameters.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here are ways of improving the execution speed of byte-compiled lisp programs.
memq
, assq
or
assoc
is even faster than explicit iteration. It may be worth
rearranging a data structure so that one of these primitive search
functions can be used.
byte-compile
property. If the property is non-nil
, then the function is
handled specially.
For example, the following input will show you that aref
is
compiled specially (@pxref{Array Functions}) while elt
is not
(@pxref{Sequence Functions}):
(get 'aref 'byte-compile) ⇒ byte-compile-two-args
(get 'elt 'byte-compile) ⇒ nil
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here are some tips for the writing of documentation strings.
The documentation string can have additional lines which expand on the details of how to use the function or variable. The additional lines should be made up of complete sentences also, but they may be filled if that looks good.
However, rather than simply filling the entire documentation string, you can make it much more readable by choosing line breaks with care. Use blank lines between topics if the documentation string is long.
/
refers to its second argument as ‘DIVISOR’.
Also use all caps for meta-syntactic variables, such as when you show the decomposition of a list or vector into subunits, some of which may be variable.
t
and nil
without single-quotes.
forward-char
. This will usually be
‘C-f’, but if the user has moved key bindings, it will be the
correct key for that user. @xref{Keys in Documentation}.
It is not practical to use ‘\\[…]’ very many times, because display of the documentation string will become slow. So use this to describe the most important commands in your major mode, and then use ‘\\{…}’ to display the rest of the mode’s keymap.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
We recommend these conventions for where to put comments and how to indent them:
Comments that start with a single semicolon, ‘;’, should all be
aligned to the same column on the right of the source code. Such
comments usually explain how the code on the same line does its job. In
Lisp mode and related modes, the M-; (indent-for-comment
)
command automatically inserts such a ‘;’ in the right place, or
aligns such a comment if it is already inserted.
(The following examples are taken from the Emacs sources.)
(setq base-version-list ; there was a base (assoc (substring fn 0 start-vn) ; version to which file-version-assoc-list)) ; this looks like ; a subversion
Comments that start with two semicolons, ‘;;’, should be aligned to the same level of indentation as the code. Such comments are used to describe the purpose of the following lines or the state of the program at that point. For example:
(prog1 (setq auto-fill-function … … ;; update mode-line (force-mode-line-update)))
These comments are also written before a function definition to explain what the function does and how to call it properly.
Comments that start with three semicolons, ‘;;;’, should start at the left margin. Such comments are not used within function definitions, but are used to make more general comments. For example:
;;; This Lisp code is run in Emacs ;;; when it is to operate asa server ;;; for other processes.
Comments that start with four semicolons, ‘;;;;’, should be aligned to the left margin and are used for headings of major sections of a program. For example:
;;;; The kill ring
The indentation commands of the Lisp modes in Emacs, such as M-;
(indent-for-comment
) and <TAB> (lisp-indent-line
)
automatically indent comments according to these conventions,
depending on the the number of semicolons. See Manipulating Comments in The GNU Emacs Manual.
If you wish to “comment out” a number of lines of code, use triple semicolons at the beginnings of the lines.
Any character may be included in a comment, but it is advisable to precede a character with syntactic significance in Lisp (such as ‘\’ or unpaired ‘(’ or ‘)’) with a ‘\’, to prevent it from confusing the Emacs commands for editing Lisp.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Emacs 19 has conventions for using special comments in Lisp libraries to divide them into sections and give information such as who wrote them. This section explains these conventions. First, an example:
;;; lisp-mnt.el --- minor mode for Emacs Lisp maintainers ;; Copyright (C) 1992 Free Software Foundation, Inc.
;; Author: Eric S. Raymond <esr@snark.thyrsus.com> ;; Maintainer: Eric S. Raymond <esr@snark.thyrsus.com> ;; Created: 14 Jul 1992 ;; Version: 1.2
;; Keywords: docs ;; This file is part of GNU Emacs. copying conditions…
The very first line should have this format:
;;; filename --- description
The description should be complete in one line.
After the copyright notice come several header comment lines, each beginning with ‘;;; header-name:’. Here is a table of the conventional possibilities for header-name:
This line states the name and net address of at least the principal author of the library.
If there are multiple authors, you can list them on continuation lines
led by ;;<TAB>
, like this:
;; Author: Ashwin Ram <Ram-Ashwin@cs.yale.edu> ;; Dave Sill <de5@ornl.gov> ;; Dave Brennan <brennan@hal.com> ;; Eric Raymond <esr@snark.thyrsus.com>
This line should contain a single name/address as in the Author line, or an address only, or the string “FSF”. If there is no maintainer line, the person(s) in the Author field are presumed to be the maintainers. The example above is mildly bogus because the maintainer line is redundant.
The idea behind the ‘Author’ and ‘Maintainer’ lines is to make possible a Lisp function to “send mail to the maintainer” without having to mine the name out by hand.
Be sure to surround the network address with ‘<…>’ if you include the person’s full name as well as the network address.
This optional line gives the original creation date of the file. For historical interest only.
If you wish to record version numbers for the individual Lisp program, put them in this line.
In this header line, place the name of the person who adapted the library for installation (to make it fit the style conventions, for example.
This line lists keywords for the finder-by-keyword
help command.
This field is important; it’s how people will find your package when
they’re looking for things by topic area.
Just about every Lisp library ought to have the ‘Author’ and ‘Keywords’ header comment lines. Use the others if they are appropriate. You can also put in header lines with other header names—they have no standard meanings, so they can’t do any harm.
We use additional stylized comments to subdivide the contents of the library file. Here is a table of them:
This begins introductory comments that explain how the library works. It should come right after the copying permissions.
This begins change log information stored in the library file (if you store the change history there). For most of the Lisp files distributed with Emacs, the change history is kept in the file ‘ChangeLog’ and not in the source file at all; these files do not have a ‘;;; Change log:’ line.
This begins the actual code of the program.
This is the footer line; it appears at the very end of the file. Its purpose is to enable people to detect truncated versions of the file from the lack of a footer line.
[Top] | [Contents] | [Index] | [ ? ] |
This document was generated on January 16, 2023 using texi2html 5.0.
The buttons in the navigation panels have the following meaning:
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ << ] | FastBack | Beginning of this chapter or previous chapter | 1 |
[ < ] | Back | Previous section in reading order | 1.2.2 |
[ Up ] | Up | Up section | 1.2 |
[ > ] | Forward | Next section in reading order | 1.2.4 |
[ >> ] | FastForward | Next chapter | 2 |
[Top] | Top | Cover (top) of document | |
[Contents] | Contents | Table of contents | |
[Index] | Index | Index | |
[ ? ] | About | About (help) |
where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:
This document was generated on January 16, 2023 using texi2html 5.0.